package com.zc.redisstream.listener;

import com.zc.domain.EnergyAlarm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.stereotype.Component;

/**
 * @author zhangcheng
 * @date 2022/9/5 21:56
 * @description
 */
@Component
public class EnergyAlarmStreamListener implements StreamListener<String, ObjectRecord<String, EnergyAlarm>> {

    private final Logger logger = LoggerFactory.getLogger(EnergyAlarmStreamListener.class);


    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(ObjectRecord<String, EnergyAlarm> message) {

        try {
            String stream = message.getStream();
            RecordId messageId = message.getId();
            EnergyAlarm valueMap = message.getValue();
            logger.info("用能阈值告警消息处理: streamKey: {}, message id: {}, valueMap: {}", stream, messageId, valueMap);


        } catch (Exception e) {
            logger.error("用能告警处理异常", e);
        } finally {
            redisTemplate.opsForStream().delete(message);
        }
    }
}
